package com.swak.vertx.protocol.http;

import com.swak.vertx.Vertxs;

import io.vertx.core.http.HttpConnection;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.net.impl.ConnectionBase;
import io.vertx.ext.web.RoutingContext;

public class RoutingContexts {

	/**
	 * 当前运行的上下文
	 * 
	 * @return
	 */
	public static ContextInternal context(RoutingContext context) {
		HttpConnection conn = context.request().connection();
		if (conn instanceof ConnectionBase) {
			ContextInternal connContext = ((ConnectionBase) conn).getContext();
			ContextInternal currContext = Vertxs.context();
			return (connContext != null && (currContext == null || connContext != currContext)) ? connContext : null;
		}
		return null;
	}
}
